
<template>
  <div class="rate">
    <div class="rate-top">
      <div class="top-left">
        <p>{{ parseFloat(temp).toFixed(1) }}</p>
        <p>综合评分</p>
        <p>高于周边商家96%</p>
      </div>
      <div class="top-right">
        <div class="right-item">
          <div>服务态度</div>
          <div>
            <p>
              <span
                class="star-item"
                v-for="(star, index) in starScore"
                :key="index"
                :class="star"
              ></span>
            </p>
            <p class="score">{{ parseFloat(temp).toFixed(1) }}</p>
          </div>
        </div>
        <div class="right-item">
          <div>菜品质量</div>
          <div>
            <p>
              <span
                class="star-item"
                v-for="(star, index) in starScore"
                :key="index"
                :class="star"
              ></span>
            </p>
            <p class="score">{{ parseFloat(temp).toFixed(1) }}</p>
          </div>
        </div>
        <div class="right-item">
          <div>送达时间</div>
          <div>
            <p class="time">20min</p>
          </div>
        </div>
      </div>
    </div>
    <div class="rate-center">
      <div class="tab">
        <label
          @click="clickTab('全部')"
          :class="curName == '全部' ? 'selected' : ''"
          >全部{{ rateList.length }}</label
        >
        <label
          @click="clickTab('满意')"
          :class="curName == '满意' ? 'selected' : ''"
          >满意{{ satisfied.length }}</label
        >
        <label
          @click="clickTab('不满意')"
          :class="curName == '不满意' ? 'selected' : ''"
          >不满意{{ dissatisfied.length }}</label
        >
      </div>
    </div>

    <div class="rate-info">
      <ul>
        <li v-for="(item, index) in Dynamiclist" :key="index">
          <img :src="item.avatar" />
          <div class="info-right">
            <div class="username">
              <h3>{{ item.username }}</h3>
              <span>2022-03-28 20:00:00</span>
            </div>
            <div class="rate-star">
              <p>
                {{
                  item.deliveryTime == "" ? "" : item.deliveryTime + "分钟送达"
                }}
              </p>
            </div>
            <div class="rate-text">
              <p>{{ item.text == "" ? "该用户未评价" : item.text }}</p>
            </div>
            <div class="recommend">
              <span v-for="(child, index) in item.recommend" :key="index">
                {{ child }}
              </span>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { shopratings } from "../../api/api";
// import { getTime } from "../../utils/utils";
export default {
  data() {
    return {
      rateList: [], //评价页面的用户列表
      satisfied: [], //满意用户
      dissatisfied: [], //不满意用户
      Dynamiclist: [], //动态用户数据
      curName: "全部", //当前选中tab标签名
      temp: 4.3, //星星的评分
    };
  },
  created() {
    shopratings().then((res) => {
      // 把时间转为标准格式
      let arr = res.data.data;
      // for (let obj of arr) {
      //   obj.rateTime = getTime(obj.rateTime);
      // }
      // console.log(arr);
      // 获取总用户评价
      this.rateList = arr; //解构
      // 设置一个动态列表的数据
      this.Dynamiclist = arr;
      // 获取不满意评价的用户
      this.dissatisfied = arr.filter((item) => {
        return item.rateType == 1;
      });
      // 获取满意评价的用户
      this.satisfied = arr.filter((item) => {
        return item.rateType == 0;
      });
      // console.log(this.rateList);
      // console.log(this.dissatisfied);
    });
    //获取星星的评分
    this.temp = this.sellerList_x.score;
  },
  methods: {
    clickTab(val) {
      this.curName = val;
      switch (val) {
        case "满意":
          this.Dynamiclist = this.satisfied;
          break;
        case "不满意":
          this.Dynamiclist = this.dissatisfied;
          break;
        case "全部":
          this.Dynamiclist = this.rateList;
      }
    },
  },
  computed: {
    sellerList_x() {
      return this.$store.state.sellerList;
    },

    starScore() {
      let stars = []; //定义一个空数组来存储类名
      let score = Math.floor(this.temp * 2) / 2; //把评分转成整数或整数+0.5（如：4.7转成4.5，4.2转成4.0）
      let decimalScore = score % 1 !== 0; //取1的余数不等于0，说明有小数（显示半星）
      let integerScore = Math.floor(score); //取整数分数（显示全星）
      for (let i = 0; i < integerScore; i++) {
        stars.push("on"); //整数分数为多少，就添加多少个全星
      }
      if (decimalScore) {
        stars.push("half"); //存在小数分数，添加一个半星
      }
      while (stars.length < 5) {
        stars.push("off"); //如果星星数不满5颗，空星星填充
      }
      return stars;
    },
  },
};
</script>

<style lang="less" scoped>
.selected {
  background-color: #ffcc33;
}
.rate {
  background-color: #f4f5f7;
  flex: 1;
  overflow: scroll;
  .rate-top {
    border-top: 1px solid rgba(236, 235, 235, 0.986);
    height: 100px;
    display: flex;
    background-color: #fff;
    padding-top: 30px;
    // 评价左边部分
    .top-left {
      flex: 40%;
      height: 80px;
      text-align: center;
      border-right: 1px solid #ccc;

      p:nth-child(1) {
        font-size: 30px;
        color: #ffcc33;
        font-weight: bold;
        padding-bottom: 3px;
      }
      p:nth-child(2) {
        padding-bottom: 2px;
        font-weight: 600;
      }
      p:nth-child(3) {
        font-size: 12px;
        color: #858687;
      }
    }
    // 评价右边部分
    .top-right {
      flex: 70%;
      padding-left: 16px;
      .right-item {
        display: flex;
        div:nth-child(1) {
          flex: 35%;
          padding-bottom: 4px;
          font-weight: bold;
        }
        div:nth-child(2) {
          display: flex;
          flex: 65%;
          padding-bottom: 4px;
          .score {
            color: #ffcc33;
          }
          .time {
            color: #858687;
          }
          .star-item {
            width: 20px;
            height: 20px;
            display: inline-block;

            background: no-repeat;
          }
          .score {
            padding-left: 6px;
          }
          .star-item.on {
            background: url("../../assets/star/on.png"); //全星
          }
          .star-item.half {
            background: url("../../assets/star/half.png"); //半星
          }
          .star-item.off {
            background: url("../../assets/star/off.png"); //空星星
          }
        }
      }
    }
  }
  .rate-center {
    background-color: #fff;
    margin-top: 20px;
    display: flex;
    justify-content: center;

    .tab {
      margin-top: 20px;
      width: 90%;
      display: flex;
      height: 50px;
      border: 1px solid #ffc300;
      border-radius: 8px;
      label {
        font-weight: bold;
        font-size: 18px;
        flex: 1;
        line-height: 50px;
        text-align: center;
      }
    }
  }
  .rate-info {
    background-color: #fff;
    li {
      padding: 20px 10px 10px 10px;
      display: flex;
      // border-bottom: 1px solid #ccc;
      img {
        width: 60px;
        height: 60px;
        border-radius: 50%;
      }
      .info-right {
        flex: 1;
        padding-left: 16px;
        .username {
          display: flex;
          justify-content: space-between;
        }
        .rate-star {
          display: flex;
        }
        .rate-text {
          padding: 10px 0;
          font-size: 16px;
          line-height: 28px;
          color: black;
        }
        .recommend {
          span {
            display: inline-block;
            border: 1px solid gray;
            color: gray;
            line-height: 40px;
            height: 40px;
            padding: 0 10px;
            margin: 4px;
          }
        }
      }
    }
  }
}
</style>




